package org.needcoke.hs.http;

import org.needcoke.hs.httpclient.HttpClientInterceptor;
import org.needcoke.hs.okhttp.OkHttpInterceptor;

@FunctionalInterface
public interface HttpInterceptor {

    void intercept(Request request);


    class Builder {

        private ClientType clientType;

        private Request request;

        private HttpInterceptor interceptor;

        public Builder clientType(ClientType type) {
            this.clientType = type;
            return this;
        }

        public Builder request(Request request) {
            this.request = request;
            return this;
        }

        public Builder interceptor(HttpInterceptor interceptor) {
            this.interceptor = interceptor;
            return this;
        }

        public HttpInterceptor build() {
            if (ClientType.httpClient == clientType) {
                return new HttpClientInterceptor() {
                    @Override
                    public void intercept(Request request) {
                        interceptor.intercept(request);
                    }
                };
            } else if (ClientType.okHttp == clientType) {
                return new OkHttpInterceptor() {
                    @Override
                    public void intercept(Request request) {
                        interceptor.intercept(request);
                    }
                };
            } else {
                return null;
            }
        }

    }

    enum ClientType {
        httpClient("org.apache.httpcomponents.httpclient"),
        okHttp("com.squareup.okhttp3.okhttp");

        private final String value;

        ClientType(String value) {
            this.value = value;
        }
    }
}
